package net.stock.controller;

import net.stock.db.StockItem;
import net.stock.db.DayPrice;
import net.stock.domain.ItemPrice;
import net.stock.service.StockDao;
import net.stock.service.StockService;
import net.stock.utils.StockUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author naim
 */

@Controller(value = "/stock")
public class StockController {
    private Logger log = Logger.getLogger(getClass().getName());

    @Autowired
    private StockDao stockDao;

    @Autowired
    private StockService stockService;

    @RequestMapping(method = RequestMethod.GET, value = "/items")
    public String showAllItems(HttpServletRequest request, ModelMap model) {
        String userEmail = StockUtils.getLoggedUserEmail(request);
        Collection<String> companyNames = stockService.getCompanyNames();

        Collection<StockItem> stockItems = stockDao.getStockItems(userEmail);

        if (stockItems != null) {
            for (StockItem si : stockItems) {
                companyNames.remove(si.getName());
            }
            model.addAttribute("currentItems", stockItems);
        }

        model.addAttribute("otherItems", companyNames);
        return "items";
    }

    @RequestMapping(method = RequestMethod.POST, value = "/addItem")
    public String addItem(HttpServletRequest request) {
        String userEmail = StockUtils.getLoggedUserEmail(request);

        String name = ServletRequestUtils.getStringParameter(request, "stockItemName", null);
        Long id = ServletRequestUtils.getLongParameter(request, "stockItemId", -1);
        Float myPrice = ServletRequestUtils.getFloatParameter(request, "stockItemMyPrice", -1);
        Float low = ServletRequestUtils.getFloatParameter(request, "stockItemLow", -1);
        Float high = ServletRequestUtils.getFloatParameter(request, "stockItemHigh", -1);

        StockItem si = new StockItem();

        if (StockUtils.notEmpty(name)) {
            si.setName(name);
            si.setEmail(userEmail);
        } else {
            throw new RuntimeException("Stock Item name is null");
        }

        if (id != -1) {
            si.setId(id);
        }
        if (StockUtils.notEmpty(name)) {
            si.setName(name);
        }

        if (myPrice != -1) {
            si.setMyPrice(myPrice);
        }
        if (low != -1) {
            si.setLow(low);
        }
        if (high != -1) {
            si.setHigh(high);
        }

        stockDao.saveStockItem(si);

        return "redirect:/stock/items";
    }

    @RequestMapping(method=RequestMethod.GET, value = "/deleteItem")
    public String deleteItem(HttpServletRequest request) {
        String userEmail = StockUtils.getLoggedUserEmail(request);

        Long id = ServletRequestUtils.getLongParameter(request, "id", -1);

        if (id != -1) {
            stockDao.deleteStockItem(userEmail, id);
        }

        return "redirect:/stock/items";
    }

    @RequestMapping(method = RequestMethod.GET, value = "/latestPrice")
    public String showLatestPrice(HttpServletRequest request, ModelMap model) {
        String userEmail = StockUtils.getLoggedUserEmail(request);
        log.log(Level.INFO, "Reading StockItems for user: " + userEmail);

        Collection<StockItem> stockItems = stockDao.getStockItems(userEmail);

        List<ItemPrice> latestPrice;
        if(stockItems != null) {
            latestPrice = stockService.getItemPrices(stockItems);
            model.addAttribute("latestPrice", latestPrice);
            model.addAttribute("dateAndTime", stockService.getDateAndTime());
        }
        return "latestPrice";
    }

    @RequestMapping(method = RequestMethod.GET, value = "/priceHistory")
    public String showPriceHistory(HttpServletRequest request, ModelMap model) {
        String item = ServletRequestUtils.getStringParameter(request, "item", "");
        Collection<DayPrice> dayPrices = stockDao.getDayPrices(item);

        ArrayList<String> companyNames = new ArrayList<String>(stockService.getCompanyNames());
        Collections.sort(companyNames);
        model.addAttribute("companyNames", companyNames);

        model.addAttribute("item", item);
        model.addAttribute("dayPrices", dayPrices);
        return "priceHistory";
    }

    @RequestMapping(method = RequestMethod.GET, value = "/refreshPriceList")
    public String getPriceList(HttpServletRequest request, ModelMap model) {
        return "refreshPriceList";
    }


}
